package com.lz.yz.outing.controller;


import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.member.entity.PropertyInfo;
import com.lz.yz.member.service.PropertyInfoService;
import com.lz.yz.order.entity.Orders;
import com.lz.yz.order.enumeration.OrderStatus;
import com.lz.yz.order.enumeration.PayTypeStatus;
import com.lz.yz.order.service.OrdersService;
import com.lz.yz.outing.model.BalanceModel;
import com.lz.yz.outing.service.SyncLockService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/23 0023.
 */
@RestController
public class BalanceController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    HttpServletRequest request;

    @Resource
    OrdersService ordersService;

    @Autowired
    SyncLockService syncLockService;

    @Autowired
    PropertyInfoService propertyInfoService;



    @ApiOperation(value="余额支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "余额实体类", dataType = "BalanceModel", paramType = "body"),
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token")
    })
    @PostMapping("/pay/balance")
    public ReturnValue balancePay(@RequestBody BalanceModel model){
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            //控制双重点击事件
            if (!syncLockService.syncLock("balancePay/" + accountId, 1, TimeUnit.SECONDS)) {
                return ReturnValue.generateFalseReturnValue("请不要重复提交!");
            }
            Orders orderBean = ordersService.findBy("orderNo",model.getOrderNo());

            if(orderBean==null){
                return ReturnValue.generateFalseReturnValue("查不到此订单!");
            }
            if(orderBean.getStatus().equals(OrderStatus.COMPLETE.getStatusValue())){
                return ReturnValue.generateFalseReturnValue("不要重复支付!");
            }
            Orders order = new Orders();
            BeanUtils.copyProperties(orderBean,order);
            //验证资产的是否大于要支付的金额
            PropertyInfo propertyInfo = propertyInfoService.findBy("accountId",accountId);
            if(propertyInfo.getBalance().compareTo(order.getTotalPrice())<0){
                return ReturnValue.generateFalseReturnValue("余额不足!");
            }
            propertyInfoService.decreaseBalance(accountId,order.getTotalPrice());
            order.setPayType(PayTypeStatus.BALANCE.getStatusValue());
            if(!ordersService.orderNotify(order,model.getBackParams())){
                return ReturnValue.generateFalseReturnValue("余额支付订单修改失败!");
            }
            return ReturnValue.generateTrueReturnValue("余额支付成功!");
        } catch (Exception e) {
            logger.error("余额支付失败", e);
            return ReturnValue.generateFalseReturnValue("余额支付失败!");
        }
    }

}
